$(function(){

    var add_height = false;                                     // 指定全域变数
    $(".class-box-content").click(function() {
        var height = $(this).find('.class-content').height(); // 取得文章内文高度
        var height_sum = height+81;                              // 实际添加高度

        $(".class-box-content .main-word").slideUp(110);    // 预设所有文章收起
        $(this).find('.main-word').slideDown();               // 如文章最大区块点击，则展开文章区块
        if(!add_height){
            add_height = true;                                   // 如文章区块点击一次，则添加大区块高度。否则不更动
            $('.cont-list').animate({marginBottom: height_sum+'px' }, 1000);
        }

        $(".class-box").removeClass('active');                 // 所有文章区块移除class
        $(this).parent().addClass('active');                    // 点击区块的上层，添加class

        var height3 = $(this).find('.class-content').height();
        var height4 = $(this).find('.main-word').height();
        // 文章展开，仅遮盖在btn上方
        var change_height = height3+height4+40;
        if($(".class-box-cover").parent(".active").length>0){
            $(this).parent(".active").children(".class-box-cover").css('cssText','height:'+change_height+'px');
        }
    });

    $(".class-box-cover").click(function() {                  // 点击文章遮罩
        $(".class-box-content .main-word").slideUp(80);     // 所有文章内文收起
        $(this).parent().removeClass('active');                 // 并移除该父层class
    });

    select_change('grade','48','48','60','74');
    select_change('subject','48','48','60','74');
    select_change('course_type','74','48','60','74');

    function select_change(name,originlength,length2,length3,length4){
        var select_name = '[name="'+name+'"]';

        var length = $(select_name).val().length;
        if(!length) {
            $(select_name).css('cssText','background-position-x: '+originlength+'px!important;');
        }
        if(length==2){
            $(select_name).css('cssText','background-position-x: '+length2+'px!important;');
        }
        if(length==3){
            $(select_name).css('cssText','background-position-x: '+length3+'px!important;');
        }
        if(length==4){
            $(select_name).css('cssText','background-position-x: '+length4+'px!important;');
        }


        $(select_name).change(function() {
            var length = $(select_name).val().length;
            if(!length) {
                $(select_name).css('cssText','background-position-x: '+originlength+'px!important;');
            }
            if(length==2){
                $(select_name).css('cssText','background-position-x: '+length2+'px!important;');
            }
            if(length==3){
                $(select_name).css('cssText','background-position-x: '+length3+'px!important;');
            }
            if(length==4){
                $(select_name).css('cssText','background-position-x: '+length4+'px!important;');
            }
        });
    }



    word_num(".elective_center .head-title .main-title",9);          // 录播课-标题
    word_num(".elective_center .class-content .abbreviation",8);    // 录播课-副标题

    //限制字符个数
    function word_num(position,num){
        $(position).each(function(){
            var maxwidth=num;
            if($(this).text().length>maxwidth){
                $(this).text($(this).text().substring(0,maxwidth));
                $(this).html($(this).html()+'…');
            }
        });
    }

    price_var_num(".del-price .dolor",6);        // 选课中心-原价
    price_var_num(".org-price .dolor",6);        // 选课中心-特价
    //限制金额
    function price_var_num(position,num){
        $(position).each(function(){
            var price_text = $(position).text();
            var price_num = price_text.split(".");
            var maxwidth=num;
            if($(this).text().length>maxwidth){
                for(var i = 0;i < $(this).text().length; i++){
                    $(this).html(price_num[0]);
                }
            }
        });
    }











});
